package com.example.travel.controller;

import com.example.travel.entity.Leave;
import com.example.travel.entity.User;
import com.example.travel.entity.Write;
import com.example.travel.entity.bo.WriteBO;
import com.example.travel.entity.dto.WriteDTO;
import com.example.travel.service.IUserService;
import com.example.travel.util.ResultUtil;
import com.example.travel.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/UserController")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private UploadUtil uploadUtil;

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @PutMapping("/updateInformation")
    public String updateInformation(User user){
        return userService.updateInformation(user);
    }

    /**
     * 上传游记图片
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadWriteTravel")
    public ResultUtil uploadWriteTravel(MultipartFile file) throws IOException {
        String upload = uploadUtil.upload(file);
        return ResultUtil.ok(upload);
    }

    /**
     * 添加游记
     * @param write
     * @return
     */
    @PostMapping("/insertWrite")
    public boolean insertWrite(Write write){
        return userService.insertWrite(write);
    }

    /**
     * 热门游记展示
     * @param writeDTO
     * @return
     */
    @GetMapping("/selectWrite")
    public ResultUtil selectWrite(WriteDTO writeDTO) {
        PageInfo<WriteBO> pageInfo = userService.selectWrite(writeDTO);
        return ResultUtil.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 查询游记详情
     * @param writeDTO
     * @return
     */
    @GetMapping("/selectWriteParticulars")
    public WriteBO selectWriteParticulars(WriteDTO writeDTO){
        return userService.selectWriteParticulars(writeDTO);
    }

    /**
     * 添加留言
     * @param leave
     * @return
     */
    @PostMapping("/insertLeave")
    public boolean insertLeave(Leave leave){
        return userService.insertLeave(leave);
    }

    /**
     * 查询留言信息
     * @return
     */
    @GetMapping("/selectLeave")
    public List<Leave> selectLeave(){
        return userService.selectLeave();
    }

    /**
     * 修改游记
     * @param writeDTO
     * @return
     */
    @PutMapping("/updateWrite")
    public boolean updateWrite(WriteDTO writeDTO){
        return userService.updateWrite(writeDTO);
    }

    /**
     * 删除游记
     * @param writeDTO
     * @return
     */
    @DeleteMapping("/delWrite")
    public boolean delWrite(WriteDTO writeDTO){
        return userService.delWrite(writeDTO);
    }
}
